/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package babydisco;

import babydisco.Math.Complex;
import babydisco.Math.Fourier;
import babydisco.Math.NoiseFiltering;
import java.util.ArrayList;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author frans
 */
public class ECGData
{
    private ArrayList<Double>[] data;
    private double[] sampleFrequency;
    private double[] sampleSize;
    
    private ArrayList<Double>[] fourierAmplitude;
    private ArrayList<Complex>[] fourierTransformed;
    private ArrayList<Double>[] fourierFrequencies;
    
    public ECGData(ArrayList[] in, double[] samplefreq)
    {
        this.data = in;
        this.sampleFrequency = samplefreq;
        
        sampleSize = new double[data.length];
        fourierAmplitude = new ArrayList[data.length];
        fourierTransformed = new ArrayList[data.length];
        fourierFrequencies = new ArrayList[data.length];
        
        
        Fourier temp;
        
        System.out.println("Started loading first set");
        for(int i = 1; i < data.length ; i++)
        {
            sampleSize[i] = data[i].size();
            //NoiseFiltering filter = new NoiseFiltering(data[i], sampleFrequency[i]);
            temp = new Fourier(data[i], sampleFrequency[i]);
           // temp = new Fourier(filter.getWeigthDiffFiltered(), filter.getFilteredFreq());
            //data[i] = filter.getWeigthDiffFiltered();

            System.out.println("Fourier build for " + i + " set");
            fourierAmplitude[i] = temp.getAmplitude();
            fourierTransformed[i] = temp.getTransformed();
            fourierFrequencies[i] = temp.getFrequencies();
            System.out.println("Started loading " + i + " set");
        }
        
    }
    
    
    
    /**
     * 
     * @param name
     * @param part int which part of the arraylists (thorax etc., see datareader
     * @return 
     */
    public XYDataset createNormalDataset(String name, int part)
    {
        XYSeries points = new XYSeries(name);
        ArrayList<Double> work = data[part];
        double f = 0.0;
        
        for(Double a : work)
        {
            points.add(f, a.doubleValue());
            f = f + sampleFrequency[part];
        }
        
        
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(points);
        return dataset;
    }
    
    public XYDataset createFourierDataset(String name, int part)
    {
        XYSeries points = new XYSeries("Fourier Amplitude");
        ArrayList<Double> work = fourierAmplitude[part];
        double f = 0.0;
        
        for(Double a : work)
        {
            points.add(f, a.doubleValue());
            f = f + sampleFrequency[part];
        }
        
        XYSeries points2 = new XYSeries("Fourier Transformed");
        ArrayList<Complex> work2 = fourierTransformed[part];
        f = 0.0;
        
        for(Complex b : work2)
        {
            points2.add(f, Complex.absolute(b));
            f = f + sampleFrequency[part];
        }
        
        XYSeries points3 = new XYSeries("Fourier Frequencies");
        ArrayList<Double> work3 = fourierFrequencies[part];
        f = 0.0;
        
        for(Double c : work3)
        {
            points3.add(f, c.doubleValue());
            f = f + sampleFrequency[part];
        }
        
        
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(points);
        dataset.addSeries(points2);
        dataset.addSeries(points3);
        
        return dataset;
    }
}
